package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.mine.presenter.ForgetPasswordPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.ForgetPasswordViewImpl;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.MD5Utils;
import com.beiing.tianshuai.tianshuai.util.RegexUtils;
import com.netease.nim.uikit.common.util.log.LogUtil;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ForgetPasswordActivity extends BaseActivity implements ForgetPasswordViewImpl {

    private Unbinder mUnBinder;
    private Context mContext;
    private ForgetPasswordPresenter mPresenter;
    private TimeCount mCountDown;
    private static final String TAG = "ForgetPasswordActivity";

    /**
     * 账号
     */
    private String mEmail;

    /**
     * 验证码
     */
    private String mSafeCode;

    /**
     * 新密码
     */
    private String mPass;

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edit_username)
    EditText mEditUsername;
    @BindView(R.id.edit_safe_code)
    EditText mEditSafeCode;
    @BindView(R.id.btn_get_safe_code)
    TextView mGetCode;
    @BindView(R.id.edit_new_password)
    EditText mEditNewPassword;
    @BindView(R.id.btn_submit)
    TextView mBtnSubmit;
    @BindView(R.id.tv_password_note)
    TextView mTvNote;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void init() {
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        initStatusBar();
        initListener();
        initPresenter();
    }

    /**
     * 初始化沉浸式状态栏
     */
    private void initStatusBar() {
//        XStatusBarHelper.setStatusBarDarkMode(this);
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        // 获取验证码
        mGetCode.setOnClickListener(new View.OnClickListener() {
            private String mNumber;
            @Override
            public void onClick(View view) {
                mGetCode.setClickable(false);
                mNumber = mEditUsername.getText().toString();
                if (RegexUtils.checkMobile(mNumber)) {
                    LogUtils.i(mNumber, "editNumber");
                    mPresenter.getSafeCodeResult(mNumber);
                } else {
                    showShortText("手机号格式不正确");
                    mGetCode.setClickable(true);
                }
            }
        });

        mEditNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String password = editable.toString();
                if (password.length() >= 6) {
                    if (RegexUtils.checkPassWord(password)) {
                        mTvNote.setTextColor(ContextCompat.getColor(mContext, R.color.color_grey_888888));
                    } else {
                        mTvNote.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                    }
                } else {
                    mTvNote.setTextColor(ContextCompat.getColor(mContext, R.color.color_grey_888888));
                }
            }
        });

        // 提交
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEmail = mEditUsername.getText().toString();
                if (TextUtils.isEmpty(mEmail)) {
                    showShortText("请输入账号");
                    return;
                }

                mSafeCode = mEditSafeCode.getText().toString();
                if (TextUtils.isEmpty(mSafeCode)) {
                    showShortText("请输入验证码");
                    return;
                }

                mPass = mEditNewPassword.getText().toString();
                if (TextUtils.isEmpty(mPass)) {
                    showShortText("请输入新密码");
                    return;
                } else if (mPass.length() < 6) {
                    mTvNote.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                } else {
                    mTvNote.setTextColor(ContextCompat.getColor(mContext, R.color.color_grey_888888));
                }

                mPresenter.getEditPasswordResult(mEmail, mPass, mSafeCode);
                LogUtil.i(TAG,"输入参数"+mEmail+" "+mPass+" "+mSafeCode+" "+MD5Utils.getMd5Value(mPass));
            }
        });

        // 返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 初始化控制器
     */
    private void initPresenter() {
        mPresenter = new ForgetPasswordPresenter(this);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    /**
     * 找回密码 bean相同
     * @param bean
     */
    @Override
    public void onRequestSuccess(CommonBean bean) {
        LogUtils.i(bean.toString(), "edit password : ");
        int code = bean.getCode();
        switch (code) {
            case 200:
                showShortText("修改成功");
                finish();
                break;
            case 400:
                showShortText("新密码不可与旧密码相同");
                break;
            case 401:
                showShortText("服务器异常"+code);
                break;
            case 402:
                showShortText("验证码失效");
                break;
            case 405:
                showShortText("验证码错误");
                break;
            case 202:
                showShortText("新密码不可与旧密码相同");
                break;
            default:
                showShortText("修改失败");
                break;
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常"+throwable);
        LogUtil.i(TAG,"网络异常"+throwable);
        mGetCode.setClickable(true);
    }

    /**
     * 判断验证码获取情况
     * @param bean 数据
     */
    @Override
    public void getSafeCodeResult(CommonBean bean) {
        LogUtils.i(String.valueOf(bean.getData().getNumber()), "getSafeCode");
        int code=bean.getCode();
        switch (code){
            case 200:
                showShortText("获取成功");
                mCountDown = new TimeCount(60000, 1000);
                mCountDown.start();
                break;
            case 400:
                showShortText("手机格式不正确");
                mGetCode.setClickable(true);
                break;
            case 401:
                showShortText("请求频率过快");
                mGetCode.setClickable(true);
                break;
            default:
                break;
        }

    }


    /**
     * 获取验证码计时器
     */
    private class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mGetCode.setText("重新获取");
            mGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.text_login_main_color));
            mGetCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mGetCode.setClickable(false);
            long l = millisUntilFinished / 1000;
            mGetCode.setText(MessageFormat.format("{0}秒", String.valueOf(l)));
            mGetCode.setTextColor(ContextCompat.getColor(mContext, R.color.text_login_main_color));
        }
    }

    @Override
    protected void onDestroy() {
        mUnBinder.unbind();
        mPresenter.onUnsubscribe();
        if (mCountDown != null) {
            mCountDown.cancel();
        }
        super.onDestroy();
    }
}
